package com.qianfeng.test.task;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.qianfeng.test.R;
import com.qianfeng.test.bean.viewpager_bean.bean.AdInfo;
import com.qianfeng.test.bean.viewpager_bean.bean.Apptop;
import com.qianfeng.test.bean.viewpager_bean.bean.ViewPagerBean;
import com.qianfeng.test.utils.HttpUtils;
import com.qianfeng.test.utils.ImageLoaderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 15-7-27.
 */
public class ViewPagerTask extends AsyncTask<String ,Void,List<Apptop>> {
    private List<Apptop> apptopList = new ArrayList<Apptop>();
    private Context context;
    private List<AdInfo>adInfoList = new ArrayList<AdInfo>();
    private DisplayImageOptions options;
    private View[] views;
    public ViewPagerTask(View[] views,Context context) {
        super();
        this.context = context;
        this.views = views;
        options = ImageLoaderHelper.getInstance().getDisplayImageOptions(R.mipmap.ic_launcher,R.mipmap.ic_launcher);
    }

    @Override
    protected List<Apptop> doInBackground(String... params) {
        // TODO Auto-generated method stub
        String result = HttpUtils.getDataFromService(params[0]);
        if (!TextUtils.isEmpty(result)) {
            ViewPagerBean viewPagerBean = JSONObject.parseObject(result, ViewPagerBean.class);
            if (viewPagerBean != null && viewPagerBean.getData() != null) {//
                apptopList = viewPagerBean.getData().getApptopList();//
                adInfoList = viewPagerBean.getData().getAdInfoList();
            }

        }
        return apptopList;

    }

    @Override
    protected void onPostExecute(List<Apptop> apptops) {
        super.onPostExecute(apptops);
        ImageView img1 = (ImageView) views[0].findViewById(R.id.head_img1_imageView);
        ImageView img2 = (ImageView) views[1].findViewById(R.id.head_img2_imageView);
      ImageLoader.getInstance().displayImage(apptopList.get(0).getImage(),img1 , options);
        ImageLoader.getInstance().displayImage(apptopList.get(1).getImage(),img2 , options);

        for (int i = 0; i < adInfoList.size(); i++) {
            final AdInfo ad = adInfoList.get(i);
            final ImageView img = new ImageView(context);

            ImageLoader.getInstance().displayImage(ad.getImage(),
                    img, options);

        }

    }
}